package test.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.Bag;
import org.apache.commons.collections.BagUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;

public class RemoveUnusedCode {
	@Test
	public void test() throws Exception {
		// test();
		String encoding = "UTF-8";
		String rootPath = "/msata/wondersTrunk";
//		String projectName="fdWebOperation";
//		String projectName="fdWebSup";
//		String projectName="fdAppSupervisor";
//		String projectName="fdAppPublic";
		String projectName="fdPushMessage";
		
//		deleteUnusedFiles(encoding,rootPath);
		processReportA(encoding, rootPath, projectName);
//		processReportB(encoding, rootPath);
		
		deleteEmptyFolder(rootPath,projectName);
		
	}

	private void deleteEmptyFolder(String rootPath, String projectName) {
		String path = Joiner.on(File.separator).join(rootPath,projectName,ReportProcessorConstant.SRC_PATH);
		FluentIterable<File> it = Files.fileTreeTraverser().postOrderTraversal(new File(path));
		for (File file : it) {
//				System.out.println(file.getAbsolutePath());
			if(file.exists() && file.isDirectory() && file.list().length==0){
//				System.out.println(file.getAbsolutePath());
				file.delete();
			}
		}
		
	}

	private void deleteUnusedFiles(String encoding, String rootPath) throws Exception {
		ReportLogicalWriter   deleteUnsedFilesWriter = new DeleteUnsedFilesWriter();
		ReportInfoReader  noreferenceInfoReader = new NoReferenceInfoReader();
		final String reportFileC = "/home/mike/git/JavaSecret/Test/src/main/resources/c.txt";
		new ReportProcessor(noreferenceInfoReader,deleteUnsedFilesWriter,reportFileC).processReport(encoding, rootPath);
	}

	private void processReportB(String encoding, String rootPath) throws IOException, Exception {
		System.out.println("============================  processReportB  ============================");
		ReportReaderB readerB = new ReportReaderB();
		ReportLogicalWriter writerB = new ReportMethodCleaner();
		final String reportFileB = "/home/mike/git/JavaSecret/Test/src/main/resources/b.txt";
		new ReportProcessor(readerB,writerB,reportFileB).processReport(encoding, rootPath);
	}

	private void processReportA(String encoding, String rootPath, String projectName) throws IOException, Exception {
		System.out.println("============================  processReportA  ============================");
		ReportReaderA readerA = new ReportReaderA();
		ReportLogicalWriter writerA = new ReportMethodCleaner();
		final String reportFileA = "/home/mike/git/JavaSecret/Test/src/main/resources/a.txt";
		rootPath = rootPath+"/"+projectName+ReportProcessorConstant.SRC_PATH;
		new ReportProcessor(readerA,writerA,reportFileA).processReport(encoding, rootPath);
//		new ReportAProcessor().processReport(encoding, rootPath, projectName);
	}

//	private static void main() {
//		Bag bag = new HashBag();
//		bag.add(1);
//		bag.add(1);
//		bag.add(2);
//		bag.add(2);
//		bag.add(3);
//		bag.add(3);
//		bag.add(3);
//		System.out.println(bag.toString());
//		System.exit(1);
//	}
}
